Matplotlib Animation Example

After several tries which did not work: This required installing the latest matching version of ffmpeg from ( https://launchpad.net/~jon-severinsson/+archive/ffmpeg ) on Ubuntu 12.04 and then passing the extra arguments extra_args=['-acodec', 'libfaac', '-vcodec', 'libx264'] to an explicitly instantiated FFMpegWriter instead of using animation.save(...)

Here is the ffmpeg version I finally installed:


In [3]:
!ffmpeg -version


ffmpeg version 0.10.11-7:0.10.11-1~precise1
built on Feb  6 2014 16:53:05 with gcc 4.6.3
configuration: --arch=amd64 --disable-stripping --enable-pthreads --enable-runtime-cpudetect --extra-version='7:0.10.11-1~precise1' --libdir=/usr/lib/x86_64-linux-gnu --prefix=/usr --enable-bzlib --enable-libdc1394 --enable-libfreetype --enable-frei0r --enable-gnutls --enable-libgsm --enable-libmp3lame --enable-librtmp --enable-libopencv --enable-libopenjpeg --enable-libpulse --enable-libschroedinger --enable-libspeex --enable-libtheora --enable-vaapi --enable-vdpau --enable-libvorbis --enable-libvpx --enable-zlib --enable-gpl --enable-postproc --enable-libcdio --enable-x11grab --enable-libx264 --shlibdir=/usr/lib/x86_64-linux-gnu --enable-shared --disable-static
libavutil      51. 35.100 / 51. 35.100
libavcodec     53. 61.100 / 53. 61.100
libavformat    53. 32.100 / 53. 32.100
libavdevice    53.  4.100 / 53.  4.100
libavfilter     2. 61.100 /  2. 61.100
libswscale      2.  1.100 /  2.  1.100
libswresample   0.  6.100 /  0.  6.100
libpostproc    52.  0.100 / 52.  0.100

In [29]:
import numpy as np
import matplotlib
#matplotlib.use("Agg")
import matplotlib.pyplot as plt
from math import sin,cos,pi
import matplotlib.animation as manimation

# If we set matplotlib logging level to debug we get more meaningful error reports if the FFMPegWriter fails.
# but we have to set it to "helpful" or "silent" in order to prevent UnsupportedOperation: IOStream has no fileno
# errors

#matplotlib.verbose.set_level('debug')
matplotlib.verbose.set_level('helpful')

FFMpegWriter = manimation.writers['ffmpeg']
metadata = dict(title='Movie Test', artist='Matplotlib',
        comment='Movie support!')
writer = FFMpegWriter(fps=15, metadata=metadata, extra_args=['-acodec', 'libfaac', '-vcodec', 'libx264'])

x0,y0 = 0, 0
fig = plt.figure()



with writer.saving(fig, "writer_test4.mp4", 100):
    for i in range(100):
        x0 += 0.1 * np.random.randn() + 0.05*sin(i * 2.0 * pi / 100)
        y0 += 0.1 * np.random.randn() + 0.05*cos(i * 2.0 * pi / 100)
        fig.clear()
        plt.xlim(-5, 5)
        plt.ylim(-5, 5)
        l, = plt.plot(x0, y0, 'k-o')

        #l.set_data(x0, y0)
        writer.grab_frame()


MovieWriter.run: running command: ffmpeg -f rawvideo -vcodec rawvideo -s 1100x800 -pix_fmt rgba -r 15 -loglevel quiet -i pipe: -vcodec mpeg4 -acodec libfaac -vcodec libx264 -metadata comment=Movie support! -metadata artist=Matplotlib -metadata title=Movie Test -y writer_test4.mp4

Displaying Videos in IPython Notebooks

This is how we can embed a video as an inline resource into an IPython Notebook.


In [30]:
from IPython.display import HTML
from base64 import b64encode
video = open("writer_test4.mp4", "rb").read()
video_encoded = b64encode(video).decode('ascii')
video_tag = '<video controls alt="test" src="data:video/x-m4v;base64,{0}">'.format(video_encoded)
HTML(data=video_tag)


Out[30]: